package tree.easy;

import tree.TreeNode;

/**
 * 另一棵树的子树
 */
public class LeetCode572 {

    boolean res = false;

    public boolean isSubtree(TreeNode root, TreeNode subRoot) {

        if (root == null) {
            return false;
        }

        if (root.val == subRoot.val) {
            if (!res) {
                res = isSameTree(root, subRoot);
            }
        }
        isSubtree(root.left, subRoot);
        isSubtree(root.right, subRoot);
        return res;
    }

    public boolean isSameTree(TreeNode root, TreeNode subRoot) {

        if (subRoot == null && root == null) {
            return true;
        } else if (subRoot == null || root == null) {
            return false;
        } else if (root.val != subRoot.val) {
            return false;
        }

        return isSameTree(root.left, subRoot.left) && isSameTree(root.right, subRoot.right);

    }
}
